<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        .container {
            width: 350px;
            height: 300px;
            margin: 0 auto;
            /* border: 1px black solid; */
            text-align: center;
        }

        .grey {
            color: grey;
        }

        .container .row {
            width: 350px;
            height: 40px;

            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .container .row input {
            width: 260px;
            height: 30px;
        }

        #submit {
            width: 350px;
            height: 40px;
            background-color: orange;
            color: white;
            border: none;
            margin: 10px;
            border-radius: 5px;
            font-size: 20px;
        }
    </style>
</head>

<body>
<div class="container">
    <h1>留言板</h1>
    <p class="grey">输入后点击提交, 会将信息显示下方空白处</p>
    <div class="row">
        <span>谁:</span> <input type="text" name="" id="from">
    </div>
    <div class="row">
        <span>对谁:</span> <input type="text" name="" id="to">
    </div>
    <div class="row">
        <span>说什么:</span> <input type="text" name="" id="say">
    </div>
    <input type="button" value="提交" id="submit" onclick="submit()">
    <!-- <div>A 对 B 说: hello</div> -->
</div>

<script src="jquery-3.7.1.js"></script>
<script>
    // 页面加载时获取所有留言
    $(document).ready(function(){
        loadMessages();
    });

    function submit(){
        //1. 获取留言的内容
        var from = $('#from').val();
        var to = $('#to').val();
        var say = $('#say').val();
        if (from== '' || to == '' || say == '') {
            return;
        }
        
        //2. 构造要发送的数据
        var messageData = {
            from: from,
            to: to,
            message: say
        };
        
        //3. 发送AJAX请求到后端
        $.ajax({
            url: '/message/publish',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(messageData),
            success: function(response) {
                // 发布成功，重新加载留言列表
                loadMessages();
                // 清空输入框的值
                $('#from').val("");
                $('#to').val("");
                $('#say').val("");
            }
        });
    }
    
    // 加载所有留言
    function loadMessages() {
        $.ajax({
            url: '/message/getList',
            type: 'GET',
            success: function(messages) {
                // 清空现有留言显示
                $('.message-list').remove();
                
                // 创建留言列表容器
                var messageListHtml = '<div class="message-list">';
                
                // 遍历留言数据，构造HTML
                for (var i = 0; i < messages.length; i++) {
                    var msg = messages[i];
                    messageListHtml += '<div class="message-item">' + 
                        msg.from + ' 对 ' + msg.to + ' 说: ' + msg.message + 
                        '</div>';
                }
                
                messageListHtml += '</div>';
                
                // 添加到页面
                $('.container').append(messageListHtml);
            }
        });
    }

</script>
</body>

</html>